package com.tl.commonlibrary.ui.widget;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tl.commonlibrary.R;
import com.wang.avi.AVLoadingIndicatorView;

/**
 * Created by a on 2015/11/12.
 */
public class LoadingView extends LinearLayout {
    private TextView contentView;
    private AVLoadingIndicatorView imgView;
    private AnimationDrawable animation;

    public LoadingView(Context context) {
        super(context);
        init(context);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View root = inflater.inflate(R.layout.layout_loading_view,null);
        imgView = (AVLoadingIndicatorView) root.findViewById(R.id.loadingImageView);
        contentView = (TextView) root.findViewById(R.id.loadingContentTView);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        setLayoutParams(params);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
//        imgView = new AVLoadingIndicatorView(context);
////        imgView.setBackgroundResource(R.drawable.anim_loading);
//        imgView.setIndicator(getContext().getString(R.string.indicator_name));
////        imgView.setIndicatorColor(UIUtils.getColor(context, R.color.base_green));
////        if(imgView.getBackground() instanceof AnimationDrawable) {
////            animation = (AnimationDrawable) imgView.getBackground();
////        }
//        addView(imgView);
//        contentView = new TextView(context);
//        LayoutParams txtParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
////        txtParams.topMargin = getResources().getDimensionPixelOffset(R.dimen.spacing_30px);
//        contentView.setLayoutParams(txtParams);
//        addView(contentView);
        addView(root);
    }

    public void setText(String loadingHint) {
        contentView.setText(loadingHint);
    }

    public void setText(int loadinHintResid) {
        contentView.setText(loadinHintResid);
    }

//    public void startAnim() {
//        if (animation != null && !animation.isRunning()) {
//            animation.start();
//        }
//    }
//
//    public void stopAnim() {
//        if (animation != null && animation.isRunning()) {
//            animation.stop();
//        }
//    }
}
